﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
/// <summary>
/// Summary description for CMySendEmail
/// </summary>
public class CMySendingEmail
{
    public CMySendingEmail()
    {

        //
        // TODO: Add constructor logic here
        //
    }

    public static string GenerateID(int MinSize, int MaxSize)
    {

        string stRefID = String.Empty;

        Random random = new Random();

        int iChosenMaxSize = random.Next(MinSize, MaxSize);


        for (int x = 1; x <= iChosenMaxSize; x++)
        {

            int iCharType = random.Next(1, 3);


            switch (iCharType)
            {

                case 1:
                    {

                        stRefID += char.ConvertFromUtf32(random.Next(48, 57));

                        break;

                    }

                case 2:
                    {

                        stRefID += char.ConvertFromUtf32(random.Next(65, 90));

                        break;

                    }

                case 3:
                    {

                        stRefID += char.ConvertFromUtf32(random.Next(97, 122));

                        break;

                    }

            }



            System.Threading.Thread.Sleep(1);

        }
        return stRefID;

    }
    public static bool sendMessage(string arrEmailAddress, string subject, string content)
    {
        SmtpClient SmtpServer = new SmtpClient();
        SmtpServer.Credentials = new System.Net.NetworkCredential("raovatnhomA2@gmail.com", "websiteNhomA2");
        SmtpServer.Port = 587;
        SmtpServer.Host = "smtp.gmail.com";
        SmtpServer.EnableSsl = true;
        MailMessage mail = new MailMessage();
        String[] addr = arrEmailAddress.Split(',');
        try
        {
            mail.From = new MailAddress("0812251@gmail.com",
            "Kiet Gui Mail", System.Text.Encoding.UTF8);
            Byte i;
            for (i = 0; i < addr.Length; i++)
                mail.To.Add(addr[i]);
            mail.Subject = subject;
            mail.Body = content;
            //if (lbAttachFile.Items.Count != 0)
            //{
            //    for (i = 0; i < lbAttachFile.Items.Count; i++)
            //        mail.Attachments.Add(new Attachment(lbAttachFile.Items[i].ToString()));
            //}

            //HACK: Chưa hiểu dòng này
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mail.ReplyTo = new MailAddress(arrEmailAddress);
            SmtpServer.Send(mail);

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }

    }
}

